getPage.onclick = ()=>{
    const request = new XMLHttpRequest()
    request.open('GET','/page2')
    request.onreadystatechange=()=>{
        if(request.readyState == 4 && request.status == 200){
            const array = JSON.parse(request.response);
            array.forEach(element => {
                const li = document.createElement('li')
                li.textContent = element.id
                xxx.appendChild(li)
            });
        }
    }
    request.send()
}
getJSON.onclick = ()=>{
    const request = new XMLHttpRequest
    request.open('GET','/5.json')
    request.onreadystatechange = ()=>{
        if(request.readyState == 4 && request.status >= 200 && request.status<300){
            console.log(request.response);
            const object = JSON.parse(request.response)
            console.log(object);
            myName.textContent = object.name
        }
    }
    request.send()
}
getXML.onclick = () => {
    const request = new XMLHttpRequest()
    request.open('GET','/4.xml')
    request.onreadystatechange = ()=>{
        if(request.readyState == 4 && request.status == 200){
            console.log(request.response);
        }
    }
    request.send()
}
getHTML.onclick = ()=>{
    const request = new XMLHttpRequest
    request.open('GET','/3.html')
    request.onload=()=>{
        console.log(request.response);
        const div = document.createElement('div')
        div.innerHTML = request.response
        document.body.appendChild(div)
    }
    request.onerror=()=>{

    }
    request.send()
}
getJS.onclick = ()=>{
    const request = new XMLHttpRequest()
    request.open("GET","/2.js")
    request.onload = ()=>{
        console.log(request.response);
        const script = document.createElement('script')//1 request.response
        document.body.appendChild(script)
    }
    request.onerror = ()=>{
        console.log('失败了')
    }
    request.send()
}
getCSS.onclick = () => {
    const request = new XMLHttpRequest()

    request.open("GET", "/style.css")

    request.onreadystatechange = ()=>{
        // console.log(request.readyState);
        if(request.readyState == 4){
            //下载完成，但不知道是成功 2xx 还是失败 4xx 5xx
            console.log('下载完成');
            console.log(request.status);
            if(request.status>=200 && request.status<300){
                const style = document.createElement('style')
                //填写style内容
                style.innerHTML = request.response
                //插到头里面
                document.head.appendChild(style)
            }else{
                alert('加载CSS失败')
            }

        }
    }
    request.send()
}



